﻿<%@ WebHandler Language="C#" Class="ComAjaxHandler" %>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.SessionState;
using TaoSH.AppCode;
using System.Text;
using TaoSH.Data;
using Newtonsoft.Json;

/// <summary>
/// Summary description for LoginHandler
/// </summary>
public class ComAjaxHandler : IHttpHandler, IRequiresSessionState
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.Buffer = true;
        context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";
        context.Response.ContentType = "text/plain";

        string strAction = context.Request.QueryString["Action"] as string;
        if (string.IsNullOrEmpty(strAction))
        {
            strAction = context.Request.Form["Action"] as string;
        }
        //获取首页中的菜单index.aspx


        //获取用户的Json数据
        if (strAction == "GetUserJson")
        {
            string strReturnMSG = HttpContext.Current.Session["UserJson"] as string;
            if (strReturnMSG == null)
            {
                strReturnMSG = new JH_Auth_UserB().GetUserInfoForJS();
                CommonHelp.SessionHelper.SetSession("UserJson", strReturnMSG);
            }
            context.Response.Write(strReturnMSG);
        }
        //获取部门的Json数据
        if (strAction == "GetBranchJson")
        {
            string strReturnMSG = HttpContext.Current.Session["BranchJson"] as string;
            if (strReturnMSG == null)
            {
                strReturnMSG = new JH_Auth_UserB().GetBranchForJS();
                HttpContext.Current.Session["BranchJson"] = strReturnMSG;
            }
            context.Response.Write(strReturnMSG);
        }
        //根据表名或查询条件获取数据
        if (strAction == "GetJsonData")
        {
            string strTable = context.Request.QueryString["Table"] as string;
            string strQueryCondition = context.Request.QueryString["QueryCondition"] as string;
            DataTable dt = new DataTable();
            dt = new JH_Auth_FormBaseB().GetDTByCommand("SELECT * FROM " + strTable.FilterSpecial() + " WHERE 1=1 " + strQueryCondition.FilterSpecial());
            string strJson = JsonConvert.SerializeObject(dt);
            context.Response.Write(strJson.Replace("null", "\"\""));
        }

        //根据表名或条件删除数据
        if (strAction == "DelData")
        {
            try
            {
                string strTable = context.Request.QueryString["Table"] as string;
                string strQueryCondition = context.Request.QueryString["QueryCondition"] as string;
                new JH_Auth_FormBaseB().ExsSql(" DELETE " + strTable.FilterSpecial() + " WHERE 1=1 AND  " + strQueryCondition.FilterSpecial());
                context.Response.Write("success");
            }
            catch (Exception)
            {

                context.Response.Write("error");
            }
        }
        //根据FormCode和ID删除数据
        if (strAction == "DelFormData")
        {
            try
            {
                string strFormCode = context.Request.QueryString["FormCode"] as string;
                string strFormID = context.Request.QueryString["FormID"] as string;
                JH_Auth_FormBase FormBaseData = new JH_Auth_FormBaseB().GetEntity(d => d.FormCode == strFormCode);
                CommonHelp.FormHelp FormHelp = new CommonHelp.FormHelp();
                foreach (string ID in strFormID.Split(','))
                {
                    FormHelp.DelForm(int.Parse(ID), FormBaseData.FormTable);
                }
                context.Response.Write("success");
            }
            catch (Exception)
            {

                context.Response.Write("error");
            }

        }
        //根据附件信息
        if (strAction == "GetFiles")
        {
            string strFileIDS = context.Request.QueryString["FileIDS"] as string;
            List<JH_Auth_FormAttchg> ListFiles = new List<JH_Auth_FormAttchg>();
            ListFiles = new JH_Auth_FormAttchgB().GetEntities(" FileUrl IN ('" + strFileIDS.ToFormatLike() + "')").ToList();
            string strJson = JsonConvert.SerializeObject(ListFiles);
            context.Response.Write(strJson);

        }

    }


    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}